package com.lanou.dllo.vc.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.easemob.EMValueCallBack;
import com.easemob.chat.EMGroup;
import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou.dllo.vc.R;
import com.lanou.dllo.vc.adapter.GroupChatAdapter;

import java.util.List;


import butterknife.Bind;
import butterknife.ButterKnife;

public class GroupChatActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.createGroup)
    LinearLayout createGroup;
    @Bind(R.id.group_listView)
    ListView group_listView;

    private GroupChatAdapter adapter;
    private List<EMGroup> groups;

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what==0) {
                adapter.addData(groups);
            }
            return false;
        }
    });




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getLayout() {
        return R.layout.activity_group_chat;

    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        //隐藏软键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        createGroup.setOnClickListener(this);

    }

    @Override
    protected void initData() {
        adapter = new GroupChatAdapter(this);
        group_listView.setAdapter(adapter);
        EMGroupManager.getInstance().asyncGetGroupsFromServer(new EMValueCallBack<List<EMGroup>>() {
            @Override
            public void onSuccess(List<EMGroup> emGroups) {
                Log.i("test", Thread.currentThread().getId() + "  grouplist---->>" + emGroups);
                groups = emGroups;
                Message message = new Message();
                message.what = 0;

                handler.sendEmptyMessage(message.what);
//                adapter.addData(emGroups);
            }

            @Override
            public void onError(int i, String s) {

            }
        });






    }


    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.createGroup:
                Intent intent = new Intent(GroupChatActivity.this,CreateGroupActivty.class);

                startActivityForResult(intent,21);

                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 21){
            if (data!=null) {
                boolean isfresh = data.getBooleanExtra("isFresh",false);

                EMGroupManager.getInstance().asyncGetGroupsFromServer(new EMValueCallBack<List<EMGroup>>() {
                    @Override
                    public void onSuccess(List<EMGroup> emGroups) {
                        groups = emGroups;
                        Message message = new Message();
                        message.what = 0;
                        handler.sendEmptyMessage(message.what);
                        Log.i("lanou", "grouplist---->>" + emGroups);
                    }

                    @Override
                    public void onError(int i, String s) {

                    }
                });
            }



        }
    }
}